// 时间工具函数
export const formatTimestamp = (timestamp: string): string => {
  try {
    return new Date(timestamp).toLocaleString('zh-CN');
  } catch (error) {
    console.error('Invalid timestamp format:', timestamp);
    return new Date().toLocaleString('zh-CN');
  }
};

// 生成ISO时间戳
export const generateISOTimestamp = (): string => {
  return new Date().toISOString();
};

// 验证时间戳格式
export const isValidTimestamp = (timestamp: string): boolean => {
  try {
    const date = new Date(timestamp);
    return !isNaN(date.getTime());
  } catch (error) {
    return false;
  }
};

// 计算时间差显示
export const getTimeAgo = (timestamp: number): string => {
  // 检查timestamp是否有效
  if (!timestamp || isNaN(timestamp) || timestamp <= 0) {
    return '';
  }
  
  const now = Date.now();
  const diff = now - timestamp;
  
  // 检查时间差是否合理（不能是负数或过大）
  if (diff < 0 || diff > 365 * 24 * 60 * 60 * 1000) { // 超过1年
    return '';
  }
  
  const minutes = Math.floor(diff / 60000);
  const hours = Math.floor(diff / 3600000);
  const days = Math.floor(diff / 86400000);

  if (days > 0) return `${days}天前`;
  if (hours > 0) return `${hours}小时前`;
  if (minutes > 0) return `${minutes}分钟前`;
  return '刚刚';
};